package com.zhou.base.components.bs.common.validator.provider;

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.internal.engine.ConfigurationImpl;
import org.hibernate.validator.internal.engine.ValidatorFactoryImpl;
import org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator;

import javax.validation.Configuration;
import javax.validation.MessageInterpolator;
import javax.validation.ValidatorFactory;
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ConfigurationState;

/**
 * 支持locale根据header里的Accept-Language来动态设置
 */
public class MyHibernateValidator extends HibernateValidator {

    @Override
    public Configuration<?> createGenericConfiguration(BootstrapState state) {
        ConfigurationImpl configuration = new ConfigurationImpl(state);
        MessageInterpolator targetInterpolator = configuration.getMessageInterpolator();
        configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));
        return configuration;
    }

    @Override
    public ValidatorFactory buildValidatorFactory(ConfigurationState configurationState) {
        return new ValidatorFactoryImpl(configurationState);
    }
}
